package aplicacao.movel;

import br.usp.fsi.entity.Sessao;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import aplicacao.movel.webservice.SessaoLogada;
import aplicacao.movel.webservice.WebServiceREST;

public class LoginActivity extends ActivityGenerica{
	
	private EditText login;
	private EditText senha;
	private Button loga;
	private Button cadastra;
	private Intent menuPrincipal;
	private Intent cadastroCNPJ;
	
	
	 public void CarregaTelaInicial() {
	        setContentView(R.layout.login);
	        
	        /*Ligacao entre aplicacao e layout*/
	        
	        login = (EditText) findViewById(R.id.loginLog);
	        senha = (EditText) findViewById(R.id.senhaLog);
	        loga = (Button) findViewById(R.botao.btlogin);
	        cadastra = (Button) findViewById(R.botao.btcadastra); 
	        
	        /*Destino do Intent*/
	        menuPrincipal = new Intent (this, MenuPrincipalActivity.class);
	        cadastroCNPJ = new Intent (this, CadastroCNPJActivity.class);
	        
	        /*Acao do primeiro botao -de login-*/
	        loga.setOnClickListener(new View.OnClickListener() {
				
	        	public void onClick(View v) {
	        
	        		try {
	        			String stringLogin = login.getText().toString();
	        			String stringSenha = senha.getText().toString();
						 Sessao sessao = new WebServiceREST().logar(stringLogin, stringSenha);
						 SessaoLogada.sessao = sessao;
						 Log.i("get", sessao.toString());
						 startActivity(menuPrincipal);
					} catch (Exception e) {
						gerarToast("Login ou Senha invalidos!");
						e.printStackTrace();
					}
						
				}
			});
	 
	        cadastra.setOnClickListener(new View.OnClickListener() {
				
				public void onClick(View v) {
						startActivity(cadastroCNPJ);//jah manda o carinha pra tela de cadastro
					
				}
			});
	    }

	    @Override
	    public void onCreate (Bundle icile){
	    	super.onCreate(icile);
	    	CarregaTelaInicial();
	    }
	    

}
